package com.reservationserver.service.impl.admin;

import com.reservationpojo.domin.vo.AdminPingpongvo;
import com.reservationpojo.entity.Pingpong;
import com.reservationpojo.entity.shutdowncourt;
import com.reservationpojo.sendmessageevent.Sse;
import com.reservationserver.mapper.admin.AdminPingpongMapper;
import com.reservationserver.service.admin.AdmintPingpongService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class AdminPingpongServiceimpl implements AdmintPingpongService {

    private final AdminPingpongMapper adminPingpongMapper;

    private final RedisTemplate redisTemplate;
    /**
     * 查询任意预约记录
     * @param pingpong
     * @return
     */
    //动态sql
    @Override
    public List<Pingpong> selectreservation(AdminPingpongvo pingpong) {
        log.info("查询预约记录:{}",pingpong);
        pingpong.setPage((pingpong.getPage()-1)*10);
        return adminPingpongMapper.selectreservation(pingpong);
    }
    /**
     * 取消预约
     * @param pingpong
     */
    @Override
    public void deleteReservation(Pingpong pingpong) {
        log.info("管理员取消预约");
        pingpong.setStatus("已取消");
        String message ="您的预约已被管理员取消";
        Sse.sendMessage("pingpong"+pingpong.getReservedByid(),message);
        log.info("jian:{}","pingpong"+pingpong.getCreateTime()+pingpong.getReservedByid());
        redisTemplate.delete("pingpong"+pingpong.getCreateTime()+pingpong.getReservedByid());
        adminPingpongMapper.deleteReservation(pingpong);
    }
    @Override
    public Pingpong checkReservation(Pingpong pingpong) {
        log.info("管理员扫码入场");
        pingpong.setStatus("已签到");
        redisTemplate.delete(pingpong.getReservedByid());
        return adminPingpongMapper.checkReservation(pingpong);
    }
    @Override
    public void shutDownCourt(String courtId, LocalDateTime starttime) {
        courtId="pingpong"+courtId;
        adminPingpongMapper.shutDownCourt(courtId,starttime);
    }

    @Override
    public void openCourt(String courtId) {
        courtId="pingpong"+courtId;
        adminPingpongMapper.openCourt(courtId);
    }

    @Override
    public List<shutdowncourt> checkCourt() {
        List<shutdowncourt> list=adminPingpongMapper.checkCourt();
        List<shutdowncourt> flag=new ArrayList<>();
        if(!list.isEmpty()){
            for (shutdowncourt s : list) {
                s.setCourt(String.valueOf(s.getCourt().charAt(8)));
                flag.add(s);
            }
        }
        return flag;
    }
}
